package com.yj.audio_output_device_new.util.ble;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;

import androidx.annotation.NonNull;

import com.yj.audio_output_device_new.util.log.Log;
import com.yj.audio_output_device_new.util.permission.PermissionUtil;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

class Ble {
    private static final String TAG = Ble.class.getSimpleName();

    // Scan
    private BluetoothAdapter mBluetoothAdapter;

    private MyProfileServiceListener mMyProfileServiceListener;
    private BluetoothA2dp mBluetoothProfileA2DP;


    private final WeakReference<Context> contextWeakReference;

    @SuppressLint("MissingPermission")
    public Ble(@NonNull WeakReference<Context> mContext){
        this.contextWeakReference = mContext;
        try {
            Context context = mContext.get();
            if(context != null){
                final BluetoothManager bluetoothManager =
                        (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
                this.mBluetoothAdapter = bluetoothManager.getAdapter();

                mMyProfileServiceListener = new MyProfileServiceListener();
                mBluetoothAdapter.getProfileProxy(context, mMyProfileServiceListener, BluetoothProfile.A2DP);
            }

        }catch (Exception e){
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
        }
    }

    @SuppressLint("MissingPermission")
    protected List<HashMap<String,Object>> getA2dpConnectedDevices(){

        List<HashMap<String,Object>> devList = new ArrayList<>();

        Context context = contextWeakReference.get();
        if(context == null){
            return devList;
        }
        if(PermissionUtil.checkBleConnectPermission(context)){
            try {
                if(mBluetoothProfileA2DP == null){
                    return devList;
                }
                List<BluetoothDevice> bondedDevices = mBluetoothProfileA2DP.getConnectedDevices();

                for (BluetoothDevice d : bondedDevices) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("name", d.getName());
                    map.put("address", d.getAddress());
                    devList.add(map);
                }
            }catch (Exception e){
                Log.e(TAG, e.getMessage());
                e.printStackTrace();
            }
        }
        return devList;
    }

    protected void close(){
        if(mBluetoothAdapter != null && mBluetoothProfileA2DP != null){
            Log.d(TAG, "closeProfileProxy(): A2DP");
            mBluetoothAdapter.closeProfileProxy(BluetoothProfile.A2DP, mBluetoothProfileA2DP);
        }
    }

    @SuppressLint("MissingPermission")
    public class MyProfileServiceListener implements BluetoothProfile.ServiceListener {
        @Override
        public void onServiceConnected(int profile, BluetoothProfile bluetoothProfile) {
            if(profile == BluetoothProfile.A2DP){
                mBluetoothProfileA2DP = (BluetoothA2dp) bluetoothProfile;
            }

        }

        @Override
        public void onServiceDisconnected(int profile) {
            if(profile == BluetoothProfile.A2DP){
                mBluetoothProfileA2DP = null;
            }
        }
    }
}
